package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;

public class OrgaCheck extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		StringBuilder sb = new StringBuilder("<state>");

		PreparedStatement ps = null;
		ResultSet rs = null;
		String shortName = null;
		boolean flag = false;// 判断组织编码
		boolean flag2 = false;// 判断BOSS渠道标志
		String s_orgacode = request.getParameter("orgacode").trim();
		String s_mobileorgacode = request.getParameter("mobileorgacode");

		for (int i = 0; i < MyConstant.RD_CITY_PROVIDER.length; i++) {
			if (s_orgacode.toUpperCase().trim().equals(MyConstant.RD_CITY_PROVIDER[i])) {
				flag = true;
				break;
			}
			if (s_mobileorgacode != null && s_mobileorgacode.trim().length() > 0) {
				if (s_mobileorgacode.toUpperCase().trim().equals(MyConstant.RD_CITY_PROVIDER[i])) {
					flag2 = true;
					break;
				}
			}

		}
		if (!flag && !flag2) {
			for (int i = 0; i < MyConstant.SOCIAL_CITY_PROVIDER.length; i++) {
				if (s_orgacode.toUpperCase().trim().equals(MyConstant.SOCIAL_CITY_PROVIDER[i])) {
					flag = true;
					break;
				}
				if (s_mobileorgacode != null && s_mobileorgacode.trim().length() > 0) {
					if (s_mobileorgacode.toUpperCase().trim().equals(MyConstant.SOCIAL_CITY_PROVIDER[i])) {
						flag2 = true;
						break;
					}
				}

			}
		}
		if (!flag && !flag2) {
			String s_sql = "SELECT shortname FROM organiza where orgacode=?";

			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_orgacode.toUpperCase().trim());
			rs = ps.executeQuery();
			if (rs.next()) {
				sb.append("<value>1</value>");
				shortName = rs.getString("shortName");
			} else
				sb.append("<value>0</value>");
			rs.close();

			if (s_mobileorgacode != null && s_mobileorgacode.trim().length() > 0) {
				s_sql = "SELECT orgacode FROM mobileorgatolocal where mobileorgacode=?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_mobileorgacode.toUpperCase().trim());
				rs = ps.executeQuery();
				if (rs.next())
					sb.append("<value>1</value>");
				else
					sb.append("<value>0</value>");
				rs.close();
			}
			ps.close();
			ps = null;
		} else {
			if (flag == true)
				sb.append("<value>1</value>");
			else
				sb.append("<value>0</value>");
			if (flag2 == true)
				sb.append("<value>1</value>");
			else
				sb.append("<value>0</value>");
		}

		sb.append("<value>" + ((flag == true) ? s_orgacode : shortName) + "</value>");
		sb.append("</state>");
		out.write(sb.toString());
		return true;

	}
}
